package cn.itcast.controller;


import cn.itcast.constant.MessageConstant;
import cn.itcast.domain.OrderSetting;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.service.OrderSettingService;

import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.commons.io.FilenameUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

//工作簿
@RestController
@RequestMapping("/orderSetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;


    /**
     * 保存工作簿中的内容,两个字段
     *
     * @param excelFile
     * @return
     */
    @RequestMapping("/upload")
    public Result upload(MultipartFile excelFile) {
        try {
            //获取文件的名称
            String originalFilename = excelFile.getOriginalFilename();
            //使用FilenameUtils工具类获取后缀
            String extension = FilenameUtils.getExtension(originalFilename);
            //1.根据文件创建Workbook
            Workbook workbook = null;
            if ("xls".equals(extension)) {
                workbook = new HSSFWorkbook(excelFile.getInputStream());
            } else if ("xlsx".equals(extension)) {
                workbook = new XSSFWorkbook(excelFile.getInputStream());
            } else {
                return new Result(false, "文件上传失败");
            }
            //解析Excel表格的数据
            //2.获取sheet
            Sheet sheet = workbook.getSheetAt(0);
            //3.获取行
            int lastRowNum = sheet.getLastRowNum();
            List<OrderSetting> orderSettings = new ArrayList<OrderSetting>();
            for (int i = 1; i <= lastRowNum; i++) {
                OrderSetting ordersetting = new OrderSetting();
                //跳过标题行  int i=1,从第二行开始
                //4.获取单元格
                Row row = sheet.getRow(i);
                //5.获取单元格中的数据 (因为excel中只有两个字段)
                Date dateCellValue = row.getCell(0).getDateCellValue();
                Double numCellValue = row.getCell(1).getNumericCellValue();
                if (dateCellValue != null && numCellValue != null) {
                    ordersetting.setOrderDate(dateCellValue);
                    ordersetting.setNumber(numCellValue.intValue());
                    orderSettings.add(ordersetting);
                }
            }
            //6.完成数据保存
            orderSettingService.saveExcel(orderSettings);
            return new Result(true, MessageConstant.UPLOAD_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "文件上传失败");
        }

    }


    /**
     * 根据日期查询预约设置数据(获取指定日期所在月份的预约设置数据)
     *
     * @param date
     * @return
     */
    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date) {
        try {
            List<Map> list = orderSettingService.getOrderSettingByMonth(date);
            //获取预约设置数据成功
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            //获取预约设置数据失败
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }


    /**
     * 根据指定日期修改可预约人数
     *
     * @param orderSetting
     * @return
     */
    @RequestMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting) {
        try {
            orderSettingService.editNumberByDate(orderSetting);
            //预约设置成功 r
            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //预约设置失败
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
    }

    /**
     * 预约列表信息展示
     *
     * @return
     */
    @RequestMapping("/findMapByPage")
    public PageResult findMapByPage(@RequestBody QueryPageBean queryPageBean) {

        PageResult pageResult = orderSettingService.findMapByPage(queryPageBean);
        return pageResult;
    }
}